package controllers

import (
	"findme-api/models"
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
	"time"
)

//注册公司名接口
func (this *UserController)Registercompany(){
	FileLogs.Info("=========注册公司名========")
	companyname := this.GetString("companyname")
	departments := this.GetString("departments")
	areas := this.GetString("areas")
	type DATA struct {
		Comoanyname string
	}
	type JSONS struct {
		//必须的大写开头
		Error_code int
		DATA DATA
	}

	var companyrows []models.CompanyInfo
	o := orm.NewOrm()
	if _, err := o.Raw("SELECT * FROM company_info WHERE companyname = ?  ",companyname).QueryRows(&companyrows); err != nil {
		fmt.Println("err")
	}
	flag := false
	//如果能查到，说明公司已经存在
	for _,companyrow := range companyrows{
		FileLogs.Info("companyrow :",companyrow)
		FileLogs.Info("公司名已经被注册")
		flag = true
		break
	}


	if flag == false {
		//添加公司
		timeUnix:=time.Now().Unix()
		//fmt.Println("添加公司")
		companydata := models.CompanyInfo{Companyid: strconv.FormatInt(timeUnix,10),Companyname:companyname,Departments:departments,Areas:areas}
		id,_:= models.AddCompony(&companydata)
		fmt.Println(id)
		//this.Ctx.WriteString("test succc")
		data := &JSONS{0,DATA{companyname}}
		this.Data["json"] = data
		this.ServeJSON()
	}else {
		data := &JSONS{1,DATA{companyname}}
		this.Data["json"] = data
		this.ServeJSON()
	}
}

